package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefixUtil;
import cn.wolfcode.shop.cloud.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class VerifyCodeController {

    @Autowired
    private RedisService redisService;
    @RequestMapping("/initVerifyCodeImg")
    public void initVerifyCodeImg(String uuid, HttpServletResponse response) throws IOException {
        //生成表达式
        String exp = VerifyCodeImgUtil.generateVerifyCode();
        //表达式结果
        Integer result = VerifyCodeImgUtil.calc(exp);
        //把结果放在redis中
        redisService.set(SeckillKeyPrefixUtil.SECKILL_VERIFYCODE, uuid, result);
        BufferedImage verifyCodeImg = VerifyCodeImgUtil.createVerifyCodeImg(exp);
        //图片以流的方式进行响应给浏览器
        ImageIO.write(verifyCodeImg, "JPG", response.getOutputStream());

    }
}
